package com.example.orderassembly.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.orderassembly.service.impl.GoodOrderServiceImpl;
import com.woniu.cloud.common.exception.TicketException;
import com.woniu.cloud.common.utils.LoginHelper;
import com.woniuxy.api.GoodOrderApi;
import com.woniu.cloud.common.dto.Result;
import com.woniuxy.entity.GoodOrder;
import com.woniuxy.form.GetAfterSaleByPlatform;
import com.woniuxy.form.GetGoodOrderForm;
import com.woniuxy.form.GetOrder;
import com.woniuxy.form.InsertGoodOrder;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 商品订单表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@RestController
@Slf4j
@Api
@SuppressWarnings("all")
public class GoodOrderController implements GoodOrderApi {
    @Resource
    GoodOrderServiceImpl goodOrderService;

    /**
     *已测
     * @param indexPage  分页查询页数
     * @param pageSize  总页数
     * @return
     */
    @Override
    @GetMapping("/getGoodOrderByUserId")
    public Result getGoodOrderByUserId(Integer indexPage,Integer pageSize,Integer userId) {
        log.info("根据用户id 查询用户商品订单信息 参数 indexPage={},pageSize={}",indexPage,pageSize);
        //todo  JWT 获取用户id
        if(userId==null){
            log.info("认证错误");
            return Result.fail("参数错误",null);
        }
        IPage iPage = goodOrderService.getGoodOrderByUserId(userId,indexPage,pageSize);

        return iPage.getRecords().size()==0?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }

    @Override
    public Result getGoodOrderById(Integer id){
        GoodOrder goodOrder = goodOrderService.getGoodOrderById(id);
        return Result.success("查询成功",goodOrder);
    }


    /**
     *已测
     * @param getOrder  分页页数
     * @return
     */
    @GetMapping("/getGoodOrderByStore")
    @Override
    public Result getGoodOrderByStore(GetAfterSaleByPlatform getAfterSaleByPlatform) {
        log.info("进入到店铺获得商品订单方法,参数 getOrder={} ",getAfterSaleByPlatform);
        //todo 根据JWT获取店铺id
        Integer storeId = 1;
        if(storeId==null){
            log.info("认证错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        IPage iPage = goodOrderService.getGoodOrderByStore(storeId,getAfterSaleByPlatform);
        return iPage.getRecords().size()==0?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }

    /**
     * 已测
     * @param getAfterSaleByPlatform  分页查询页数
     * @return
     */
    @Override
    public Result getGoodOrderByPlatform(GetAfterSaleByPlatform getAfterSaleByPlatform) {
        log.info("进入到平台获取商品订单信息方法中，getOrder={}",getAfterSaleByPlatform);
        if(getAfterSaleByPlatform.getIndexPage()==null||getAfterSaleByPlatform.getPageSize()==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        IPage iPage = goodOrderService.getGoodOrderByPlatform(getAfterSaleByPlatform);
        return iPage.getRecords().size()==0?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }

    /**
     * 已测
     * @param form
     * @return
     */
    @GetMapping("/getGoodOrder")
    @Override
    public Result getGoodOrder(GetGoodOrderForm form) {
        log.info("进入到多条件查询商品订单接口 form={}",form);
        if (form == null) {
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        IPage iPage = goodOrderService.getGoodOrder(form);
        return iPage.getRecords().size()==0?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }

    /**
     * 已测
     * @param goodOrderId 订单id
     * @param status  修改的状态 0:未付款 1:已付款  2:待自提  3: 已自提 4:已发货 5:物流中  6:已签收  7:申请退货  8:申请换货  9:退货完成  10:订单完成   11:换货完成，12：未评论  13：已评论
     * @return
     */
    @Override
    public Result updateGoodOrderStatusById( Integer goodOrderId, Integer status) {
        log.info("修改商品订单的状态 goodOrderId={},status={}",goodOrderId,status);
        System.err.println("修改商品订单的状态");
        if(goodOrderId==null||status==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Boolean bool = goodOrderService.updateGoodOrderStatusById(goodOrderId,status);
        return bool?Result.success("修改成功",bool):Result.fail("修改失败",bool);
    }

    //ToDo 用户下单，生成 未付款订单   同时生成订单详情  删除购物车信息

    /**
     *  =========待测
     * @param insertGoodOrder  要生成的商品订单
     * @return
     */
    @Override
    @PostMapping("/insertGoodOrder")
    @GlobalTransactional
    public Result insertGoodOrder(InsertGoodOrder insertGoodOrder) {
        log.info("用户下单，生成 未付款订单   同时生成订单详情  删除购物车信息 参数 ={}",insertGoodOrder);
        if (insertGoodOrder==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Integer bool = goodOrderService.insertGoodOrder(insertGoodOrder);
        return bool!=null?Result.success("下单成功",bool):Result.fail("下单失败",bool);
    }

    @Override
    public Result updateGoodOrderPayType(Integer orderId, Integer payType) {
        Boolean bool = goodOrderService.updateGoodOrderPayType(orderId,payType);
        return bool?Result.success("修改成功",bool):Result.fail("修改失败",bool);

    }

    /**
     * 已测
     * @param goodOrderId 商品订单号
     * @return
     */
    @Override
    public Result updateGoodOrderStatus(Integer goodOrderId) {
        log.info("用户取消订单  把订单状态改为 取消 参数 goodOrderId={}",goodOrderId);
        if(goodOrderId==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Boolean bool = goodOrderService.updateGoodOrderStatus(goodOrderId);
        return bool?Result.success("取消成功",bool):Result.fail("取消失败",bool);
    }

    /**
     *已测
     * @param goodOrderId 商品订单号
     *
     * @return
     */
    @Override
    public Result updateGoodOrderPaied(Integer goodOrderId) {
        log.info("对该订单进行付款操作，改变订单状态为已付款 参数 goodOrderId={}",goodOrderId);
        if(goodOrderId==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Boolean bool = goodOrderService.updateGoodOrderPaied(goodOrderId);
        return bool?Result.success("付款成功",bool):Result.fail("付款失败",bool);
    }

    /**
     *已测
     * @param goodOrderId 商品订单号
     * @return
     */
    @Override
    public Result updateGoodOrderReceiving(Integer goodOrderId) {
        log.info("用户点击确认收货，将订单状态改为 已收货 参数 goodOrderId={}",goodOrderId);
        if(goodOrderId==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Boolean bool = goodOrderService.updateGoodOrderReceiving(goodOrderId);
        return bool?Result.success("收货成功",bool):Result.fail("发货失败",bool);
    }

    /**
     * 已测
     * @param goodOrderId 商品订单号
     * @return
     */
    @Override
    public Result updateGoodOrderEvaluated(Integer goodOrderId) {
        log.info("用户完成评价后，将订单状态改为 已评价 参数 goodOrderId={}",goodOrderId);
        if(goodOrderId==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Boolean bool = goodOrderService.updateGoodOrderEvaluated(goodOrderId);
        return bool?Result.success("评价成功",bool):Result.fail("评价失败",bool);
    }
}

